package com.atguigu.gulimall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ 配置类
 *
 *  容器中的 Binding,Queue,Exchange 都会自动创建（RabbitMQ没有的情况）
 *  RabbitMQ 只要有，@Bean 声明属性发生变化也不会覆盖
 * @author xiaoping
 * @create 2020/7/7 18:32
 * @since 1.0.0
 */
@Configuration
public class MyMQConfig {


    /**
     * 死信队列(延时队列)
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){
        //Queue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
        Map<String,Object> arguments = new HashMap<>();
        //设置死信路由
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        //路由键
        arguments.put("x-dead-letter-routing-key","order.release.order");
        //过期时间(毫秒)
        arguments.put("x-message-ttl", 60000);
        return new Queue("order.delay.queue",true,false,false,arguments);
    }

    /**
     * 普通队列（消费队列）
     * @return
     */
    @Bean
    public Queue orderReleaseOrderQueue(){
        return new Queue("order.release.order.queue",true,false,false,null);
    }

    /**
     * 交换机
     * @return
     */
    @Bean
    public Exchange orderEventExchange(){
        //考虑到一对多，使用主题交换机，模糊匹配的方式  TopicExchange(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments)
        return new TopicExchange("order-event-exchange", true, false);
    }

    /**
     * 死信队列绑定关系
     * @return
     */
    @Bean
    public Binding orderCreateOrderBingding(){
        /*
          Binding(String destination(目的地[队列]),
          DestinationType destinationType（目的地类型）,
          String exchange（交换机）,
          String routingKey（路由键）,
          Map<String, Object> arguments)（自定义参数）
         */
        return new Binding("order.delay.queue",Binding.DestinationType.QUEUE,"order-event-exchange","order.create.order",null);
    }

    /**
     * 普通队列绑定关系
     * @return
     */
    @Bean
    public Binding orderReleaseOrderBingding(){
        return new Binding("order.release.order.queue",Binding.DestinationType.QUEUE,"order-event-exchange","order.release.order",null);
    }

    /**
     * 订单释放直接和库存释放进行绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBingding(){
        return new Binding("stock.release.stock.queue",Binding.DestinationType.QUEUE,"order-event-exchange","order.release.other.#",null);
    }


    /**
     * 商品秒杀的订单队列
     * @return
     */
    @Bean
    public  Queue orderSeckillOrderQueue(){

        return new Queue("order.seckill.order.queue",true,false,false);
    }

    /**
     * 商品秒杀的订单队列绑定关系
     * @return
     */
    @Bean
    public  Binding orderSeckillOrderQueueBinding(){

        return new Binding("order.seckill.order.queue",Binding.DestinationType.QUEUE,"order-event-exchange","order.seckill.order",null);
    }




}
